<!-- 图片列表显示 -->
<template>
  <div v-if="imglist.length">
    <draggable v-model="imglist" class="product-img" @end="datadragEnd">
        <div class="item" v-for="(item,index) in imglist" :key="index">
          <div class="img">
            <img :src="item.src" :alt="item.alt" :title="item.title"/>
          </div>
          <div class="opt">
            <el-button type="text" @click.stop="editorAttr(index)"><i class="el-icon-edit"></i></el-button>
            <el-button type="text" @click.stop="deleteImage(index)"><i class="el-icon-delete"></i></el-button>
          </div>
        </div>
    </draggable>
    <WDialogSysImageAttr ref="wDialogImageAttr" />
  </div>
</template>

<script>
import WDialogSysImageAttr from '@/components/main/user/system/common/WDialogSysImageAttr.vue'
import draggable from 'vuedraggable'
export default {
  props: {
    imageList: {
      type: Array
    },
    resetImageList: {
      type: Function
    }
  },
  data () {
    return {
    }
  },
  components: {
    WDialogSysImageAttr,
    draggable
  },
  computed: {
    imglist: {
      get () {
        return this.imageList
      },
      set (newvalue) {
        this.resetImageList(JSON.parse(JSON.stringify(newvalue)))
      }
    }
  },
  methods: {
    // clickImage (id) {
    //   let item = getItem(id, this.imglist)
    //   if (!item) {
    //     return
    //   }
    //   window.open(item.src)
    // },
    // 拖拉排序
    datadragEnd (event) {
      // console.log('list', this.imglist)
    },
    deleteImage (index) {
      let list = []
      for (let i in this.imglist) {
        if (parseInt(i) === parseInt(index)) {
          continue
        }
        list.push(this.imglist[i])
      }
      this.resetImageList(JSON.parse(JSON.stringify(list)))
    },
    // 编辑图片属性
    editorAttr (index) {
      let item = this.imageList[index]
      if (!item) {
        return
      }

      this.$refs['wDialogImageAttr'].open({ alt: item.alt, title: item.title }, (image) => {
        this.imageList[index].alt = image.alt
        this.imageList[index].title = image.title
      })
    }
  }
}

// function getItem (id, list) {
//   return list.find((item) => {
//     if (id === item.id) {
//       return true
//     }
//     return false
//   })
// }

</script>
<style scoped lang="scss">
.product-img {
  display: flex;
  flex-flow: wrap;
  margin: 12px 0;
  .item {
    width: 180px;
    margin-right: 8px;
    position: relative;
    margin-bottom: 8px;
    .img {
      line-height: 180px;
      text-align: center;
      border: 1px solid #e5e5e5;
      height: 180px;
      cursor: move;
      img {
        max-width: 170px;
        max-height: 170px;
        vertical-align: middle;
      }
    }

    .opt {
      position: absolute;
      top: 0;
      right: 0;
      background: #409EFF;
      line-height: 26px;
      padding: 0 5px;
      display: none;
      .el-button {
        color: #fff;
        padding:3px 5px;
        margin: 0;
      }
    }
    &:hover .opt{
      display: block;
    }
  }
}
</style>
